<%@ page contentType="text/html;charset=UTF-8"%>
<%@include file="../../common/sessionValidate.jsp" %>
<%@include file="../../common/taglibs.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html lang="UTF-8">
  <head>
    <title>T_user_role信息维护</title>
    <%@ include file="../../common/meta.jsp"%>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="X-UA-Compatible" content="IE=9">
	<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <link rel="stylesheet" href="<%=basePath%>css/my-bootstrap.css" media="screen" id="tuserskin">
    <link rel="stylesheet" href="<%=basePath%>js/toastr/toastr.css">
    <link rel="stylesheet" href="<%=basePath%>css/bootstrapValidator.min.css">
    <link rel="stylesheet" href="<%=basePath%>css/font-awesome.min.css">
   	<script type="text/javascript" src="<%= basePath %>js/jquery.cookie.js"></script>
  </head>
  <style>
	hr {
	    margin-top:1px;
	    border-bottom: 1px solid #1BBC9B;
	}
  </style>
  <body>
  <div class="" style="margin-right:2px;padding-right:1px;">
  	<form class="form-horizontal" name="form1" id="form1">
    <!--------------------------信息列表：带表格面板  start--------------------------------->
	<div class="panel panel-primary">
	  <div class="panel-heading clearfix" >
	  	 <i class='icon-cogs'>&nbsp;分配操作员角色</i>
	  </div>
	  <div class="panel-body">
	    <p>操作员：
		 <span id="user" class="label label-primary"></span>&nbsp;&nbsp;对应角色列表：
		 <input type="hidden" name="userid" id="userid" value="">
		 <input type="hidden" name="id" id="id" value="${param.id}">
        </p>
	    <table id="grid" class="table table-condensed table-striped table-bordered table-hover" class="sortable" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
	     <thead id="grid_head">
	      <tr>
	          <th style="width:60px" onclick="" class="table-checkbox" ><input class="group-checkable" name="batchSelectedAll" id="batchSelectedAll" type="checkbox" onclick="selectAll2()">全选</th>
	          <th  >角色名称</th>
	      </tr>
	     </thead>
	     <tbody id="grid_body"></tbody>
	    </table>
	  </div>
	  <div class="modal-footer bg-info " id='buttonDiv'>
		<button type="button" class="btn btn-sm btn-primary"  name="addSaveButton" id="addSaveButton" onClick="saveUserRole()">保存</button>
		<button type="button" class="btn btn-sm btn-danger" data-dismiss="modal"  onClick="window.history.go(-1)">返回</button>
	  </div>
    </div>
    <!--------------------------信息列表：带表格面板 end------------------------------------>

  </form>
  </div>
  </body>
</html>

<script src="<%=basePath%>js/bootstrap.min.js"></script>
<!--<script type="text/javascript" src="js/bootstrapValidator.js"></script>-->
<script src="<%=basePath%>js/bootstrap-paginator.min.js"></script>
<script src="<%=basePath%>/js/respond.js"></script>
<script src="<%=basePath%>/js/html5.js"></script>
<script src="<%=basePath%>js/toastr/toastr.js"></script>
<script src="<%=basePath%>js/bootbox.js"></script>
<script src="<%=basePath%>js/jquery.validate.js"></script>
<!-- <script src="<%=basePath%>js/additional-methods.min.js"></script> -->
<script src="<%=basePath%>js/my-jquery-curd.js"></script>
<script src="<%= basePath %>js/tableSortTool.js" ></script>
<script src="<%= basePath %>js/calendar.js" ></script>

<script type="text/javascript">
$().ready(function(){
	//1、初始化数据显示
	var url = '<%=basePath%>t_user/getT_user?id=<%=request.getParameter("id")%>&time='+Math.random();
    //alert(url);
	$.getJSON(url, function (data) {//获取数据，并且自动填充form，自动代码待修改； @todo;
    	//setValue(data);
    	//alert($("#user").val());
    	//alert(data.username);
    	$("#user").html(data.username);
    	$("#userid").val(data.userid);
    });

	//2、初始化操作员数据
	Search(0);

}) //end funciton read;

//根据查询条件，查询列表数据并显示
function Search(startIndex){
	var pageSize = 10000;
	var startIndex = 0;
    var condition="";
	//1、初始化功能菜单数据列表
	var url = "<%=basePath%>t_role/getAllT_role?id=0&startIndex="+startIndex+"&pageSize="+pageSize+"&"+condition+"&time="+Math.random();
	//alert(url);
	$.getJSON(url, function (data){
		//1.1、表格行内容输出
		var p1id="";
	    $.each(data.items, function(i,item){
			var str="<tr>";
				str+="<td><input class='checkboxes' name='ids' id='ids"+item.id+"' type='checkbox' value='"+item.id+"'></td>";
				str+="<td>"+doNull(item.rolename)+"</td>";
			str+="</tr>";
			$("#grid_body").append(str);
			//alert(str);
		 });//end each data;

		 //2、初始化选中功能菜单，对应的checkbox为 checked状态;根据Value值设置checkbox为选中值
		 var userid =$("#userid").val();
		 //alert("userid="+userid);
		 var surl = "<%=basePath%>t_user_role/getAllT_user_role?userid="+userid+"&startIndex=0&pageSize=10000&&time="+Math.random();
		 //alert(surl);
		 $.getJSON(surl, function (data){
		 	$.each(data.items, function(i,item){
		 		//alert("历史选中的roleid="+item.roleid);
		    	$("input:checkbox[value="+item.roleid+"]").attr('checked',true);
		 	});
		 });//end getJSON2;
	})//end getJSON1;
} //end serach;

function saveUserRole(){
    //if(checkfunctionIDS2()!=false){
    	//alert("should not appear");
    	$("#addSaveButton").attr("disabled", true);
    	var url = "<%=basePath%>t_user_role/saveT_user_roles?time="+Math.random();
    	var postData = $("#form1").serializeArray();
    	//alert(postData);
        $.post(url, postData, function (data) {
        	//var data = $.parseJSON(data);
            if (data.result=="success") {
                //保存成功  1.关闭弹出层，
              	showSuccessTips("恭喜保存成功!", "提示");
              	//history.goBack();
            }
            else {
                $("#addSaveButton").removeAttr("disabled");
            	showErrorTips("保存数据失败!", "错误信息：");
            	return false;
            }
        }).error(function () {
            $("#addSaveButton").removeAttr("disabled");
        	showErrorTips("保存数据失败!", "错误信息：");
        });
    //}
}

//得到多选框选中的值
function getCheckboxValue2(){
	var checkbox = checkfunctionIDS2();
	if(checkbox!=false){
		return checkbox.value;
	}
}

//校验多选框，只能选择一条记录进行操作，适合 修改和查看按钮对应选择数据；
function   checkfunctionIDS2(){
   var obj,count,obj1;
      obj=document.getElementsByName("ids");
      count=0;
      for(i=0;i<obj.length;i++)
      {
         if(obj[i].checked==true)
         {
         	count++;
         	obj1 =obj[i];
         }
      }
      if(count<1)
      {
         //alert("请至少选择一条数据记录进行操作！");
         showErrorTips("请至少选择一条数据记录进行操作！", "错误信息：");
         return false;
      }
      //else if (count>1)
      //{
      //   alert("请选择一条记录进行操作（添加操作请选择一个父节点）！");
      //   return  false;
      //}
      return obj1;
}

//全选和反选
function selectAll2(){
	var checklist = document.getElementsByName ("ids"); //所有复选框id名字
	if(document.getElementById("batchSelectedAll").checked){ //全选按钮所在check的名称
		for(var i=0;i<checklist.length;i++)
	    {
			checklist[i].checked = 1;
	    }
	 }else{
		  for(var j=0;j<checklist.length;j++)
			  {
			     checklist[j].checked = 0;
			  }
		 }
}
</script>
